package cn.getech.data.metadata.service.impl;

import cn.getech.data.development.constant.PermissionTypeEnum;
import cn.getech.data.development.utils.DataPermissionUtil;
import cn.getech.data.metadata.mapper.CollectionTableAnalysisMapper;
import cn.getech.data.metadata.model.res.collectionTableAnalysis.CollectionTableAnalysisRes;
import cn.getech.data.metadata.service.ICollectionTableAnalysisService;
import cn.getech.system.center.utils.ShiroUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CollectionTableAnalysisServiceImpl implements ICollectionTableAnalysisService {

    @Autowired
    private CollectionTableAnalysisMapper collectionTableAnalysisMapper;


    @Override
    public List<CollectionTableAnalysisRes> coldAndHotList(Integer collectionId, Integer page, Integer pageSize) {

        //根据collectionId,查询源数据
        CollectionTableAnalysisRes meteInfo = collectionTableAnalysisMapper.getMetadataTable(collectionId);

        Map<String, Object> params = new HashMap<>();
        //所有用户都可以查询元数据
        Long userId = ShiroUtils.getUserId();
        params.put("userId", userId);
        //非超管权限查数据
//        if (!DataPermissionUtil.isAdmin(userId.intValue())) {
//            List<Integer> ids = DataPermissionUtil.getDataIds(userId.intValue(),
//                    PermissionTypeEnum.METADATA_MANAGEMENT.getType(),
//                    10);
//            if (CollectionUtils.isEmpty(ids)) {
//                ids.add(-1);
//            }
//            params.put("ids", ids);
//            List<Integer> offIds = DataPermissionUtil.getDataIds(userId.intValue(),
//                    PermissionTypeEnum.STORE.getType(),
//                    10);
//            if (CollectionUtils.isEmpty(offIds)) {
//                offIds.add(-1);
//            }
//            params.put("offIds", offIds);//离线表的id
//            List<Integer> onlineIds = DataPermissionUtil.getDataIds(userId.intValue(),
//                    PermissionTypeEnum.REAL_TABLE.getType(),
//                    10);
//            if (CollectionUtils.isEmpty(onlineIds)) {
//                onlineIds.add(-1);
//            }
//            params.put("onlineIds", onlineIds);//实时表内部表
//            List<Integer> resourceIds = DataPermissionUtil.getDataIds(PermissionTypeEnum.RESOURCE.getType(),
//                    10);
//            if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(resourceIds)) {
//                resourceIds.add(-1);//无权限
//            }
//            params.put("sourceIds", resourceIds);//实时表的外部表
//        }else{
//            params.put("list", true);//列表查询
//        }

        List<CollectionTableAnalysisRes> ctaList = new ArrayList<>();
        List<Integer> idList = new ArrayList<>();

        ctaList.add(meteInfo);
        //根据collectionId,查询上游表
        getMetadataTableList(params,idList,ctaList,collectionId,meteInfo.getTableInfoId(),"up");

        //根据collectionId,查询下游表
        idList.clear();
        getMetadataTableList(params,idList,ctaList,collectionId,meteInfo.getTableInfoId(),"down");

//        //整合数据，分页返回
//        Page<CollectionTableAnalysisRes> data = new Page<>(page,pageSize);
//        data.setTotal(ctaList.size());
//        ctaList = ctaList.stream().skip((page - 1) * pageSize).limit(pageSize).
//                collect(Collectors.toList());
        //data.setRecords(ctaList);

        return ctaList;
    }

    private void getMetadataTableList(Map<String, Object> params,List<Integer> idList,List<CollectionTableAnalysisRes> ctaList,Integer collectionId,Integer tableInfoId,String flag) {

        //通过父类目ID获取子类目集合
        List<CollectionTableAnalysisRes> dataList = collectionTableAnalysisMapper.getMetadataTableList(params,collectionId,tableInfoId,flag);
        if (dataList == null || dataList.size() == 0){
            return;
        }
        Iterator<CollectionTableAnalysisRes> iterator = dataList.iterator();
        while(iterator.hasNext()){
            CollectionTableAnalysisRes data = iterator.next();
            if(idList.contains(data.getTableId())){
                iterator.remove();
                continue;
            }
            idList.add(data.getTableId());
        }
        ctaList.addAll(dataList);

        for (CollectionTableAnalysisRes res : dataList) {
            getMetadataTableList(params,idList,ctaList,res.getTableId(),res.getTableInfoId(),flag);
        }

    }
}
